<?php
$orderServices = new OrdersService();
$orderDetailService = new OrderDetailsService();
$articlerServices = new ArticleService();
$factoryServices = new FactoryServices();
$userService = new UserService();
$orderdetailstempService = new OrderDetailstempService();
$toEmail = $glb['contactEmail'];
$host = Tool::decode($smtp['host'], "sTunnInVietnAm@2012#");
$port = Tool::decode($smtp['port'], "sTunnInVietnAm@2012#");
$user = Tool::decode($smtp['user'], "sTunnInVietnAm@2012#");
$pass = Tool::decode($smtp['pass'], "sTunnInVietnAm@2012#");
$emailService = new EmailService($host, $port, $user, $pass);
$emailService->setSiteURL($glb['websiteURL']);
switch ($action) {
	case "complainF":
		if($permit == true)
		{
			if(isset($_POST['orderID']) && $_POST['orderID'] != '' && $_POST['complain'] != '')
			{
				$orderID = $_POST['orderID'];
				$complainText = addslashes($_POST['complain']);
				$type = 11;
				$orderDomain = $orderServices->getOrderDomainByOrderID($context, $result, $orderID);
				$orderDomain->setComplain($complainText);
				$orderDomain->setType($type);
				print_r($orderDomain);
				$orderServices->updateOrderDomain($context, $result, $orderDomain);
				header("Location:index.php?func=order&opt=listOrderF");
			}
		}
		else
		{
			header("Location:index.php?func=dash");
		}
	break;
			
	case "confirmF":
		if($permit == true)
		{
			if(isset($_POST['shipmentDate']))
			{
				
				$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
				$orderDetailArr = $order->getOrderDetail();
				foreach($orderDetailArr as $orderDetail)
				{	
					
					$orderDetailDomain = $orderDetail->getOrderdetailsDomain();
					$subOrderNumber = $orderDetailDomain->getSubOrderNumber();
					$orderDetailID = $orderDetailDomain->getID();					
					$orderDetailDomain->setShipmentDate($_POST['shipmentDate']);
					$orderDetailDomain->setCbm($_POST['cbm'][$orderDetailID]);
					$orderDetailDomain->setStt(1);
				}				
				$orderServices->updateFullOrder($context, $result, $order);
				$order->getOrdersDomain()->setOrderNumber($order->getOrdersDomain()->getOrderNumber().$subOrderNumber);
				//create PDF file
				$reportService = new ReportService();
				$reportService->setSiteURL($glb['websiteURL']);
				$file = $reportService->generatePDFFile($context, $result, $order);
				//send Email notification to BArt
				$fileName = $order->getOrdersDomain()->getOrderNumber().'.pdf';
				$PIID = $order->getOrdersDomain()->getOrderID();
				$orderDetails = $order->getOrderDetail();
				$fromEmail = $orderDetails[0]->getFactory()->getFactoryDomain()->getEmail();
				$emailService->sendEmailProformaInvoiceFromFactory($context, $result, $fromEmail, $toEmail, $PIID, $fileName, $file);
			}
			header("Location:index.php?func=purchaseOrder&opt=POListFact");
		}
		else
		{
			header("Location:index.php?func=dash");
		}
	break;
	
	case "editPOF":
		if($permit == true)
		{
			if(isset($_POST['shipmentDate']))
			{
				
				$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
				$orderDetailArr = $order->getOrderDetail();
				foreach($orderDetailArr as $orderDetail)
				{
				
					$orderDetailDomain = $orderDetail->getOrderdetailsDomain();
					$orderDetailID = $orderDetailDomain->getID();					
					$orderDetailDomain->setShipmentDate($_POST['shipmentDate']);
					$orderDetailDomain->setCbm($_POST['orderDetail'][$orderDetailID]['cbm']);
					$orderDetailDomain->setStt(2);
				}
				$orderServices->updateFullOrder($context, $result, $order);
				foreach($orderDetailArr as $orderDetail)
				{
				
					$orderDetailDomain = $orderDetail->getOrderdetailsDomain();
					$orderDetailID = $orderDetailDomain->getID();					
					$orderDetail->getArticle()->getArticleDomain()->setDescription($_POST['orderDetail'][$orderDetailID]['description']);
					$orderDetail->getArticle()->getArticleDomain()->setWidth($_POST['orderDetail'][$orderDetailID]['width']);
					$orderDetail->getArticle()->getArticleDomain()->setHeight($_POST['orderDetail'][$orderDetailID]['height']);
					$orderDetail->getArticle()->getArticleDomain()->setDepth($_POST['orderDetail'][$orderDetailID]['depth']);
					$orderDetailDomain->setPriceFact(Tool::convertMoneyToNumber($_POST['orderDetail'][$orderDetailID]['priceFact']));
					
					$data = array(
							'ID' => $orderDetailID,
							'orderID' => $_GET['id'],
							'articleID' => $orderDetail->getArticle()->getArticleDomain()->getID(),
							'factoryID' => $orderDetail->getFactory()->getUserDomain()->getID(),
							'subOrderNumber' => $orderDetailDomain->getSubOrderNumber(),
							'priceFact' => $orderDetailDomain->getPriceFact(),
							'shipmentDate' => $orderDetailDomain->getShipmentDate(),
							'cbm' => $orderDetailDomain->getCbm(),
							'width' => $_POST['orderDetail'][$orderDetailID]['width'],
							'height' => $_POST['orderDetail'][$orderDetailID]['height'],
							'depth' => $_POST['orderDetail'][$orderDetailID]['depth'],
							'description' => $_POST['orderDetail'][$orderDetailID]['description']
					);
					$orderdetailstempDomain = new OrderdetailstempDomain($data);
					$orderdetailstempService->create($context, $result, $orderdetailstempDomain);
				}
				//create PDF file
				$reportService = new ReportService();
				$reportService->setSiteURL($glb['websiteURL']);
				$file = $reportService->generatePDFFile($context, $result, $order);
				//send Email notification to BArt
				$fileName = $order->getOrdersDomain()->getOrderNumber().'.pdf';
				$PIID = $order->getOrdersDomain()->getOrderID();
				$orderDetails = $order->getOrderDetail();
				$fromEmail = $orderDetails[0]->getFactory()->getFactoryDomain()->getEmail();
				$emailService->sendEmailProformaInvoiceFromFactory($context, $result, $fromEmail, $toEmail, $PIID, $fileName, $file);
			}
			header("Location:index.php?func=purchaseOrder&opt=POListFact");
		}
		else
		{
			header("Location:index.php?func=dash");
		}
	break;
			
	default:
		echo "khong co tinh nang nay";
		break;
}
?>